 App({
    domain: "https://wj2021.tjjwwl.com/",
    resPath: "https://wj2021.tjjwwl.com/ssm/plat/feet/res/",
    //domain: "https://xcxrun.tjjwwl.com/",
    //resPath: "https://xcxrun.tjjwwl.com/ssm/plat/feet/res/",
    openid: "",
    config: null,
    loginSuccess: false,
    userInfo: null,
    appid:"wx3402496420e64d05",
    
    //appid:"wx4db5254b1e989706",
    indexShow: false,// 是否已经显示过index页面
    onLaunch() {
        this.getOpenId();
        this.getConfig();
    },
    getConfig() {
      return new Promise((rs, rj)=> {
        // if(this.config) {
        //   rs(this.config);
        // }
        // else {
          wx.request({
              url: this.resPath + "json/config.json",
              method: "GET",
              success: (res)=> {
                console.log(res)
                this.config = res.data;
                rs(res.data);
              },
              fail (e) {
                rj(e)
              }
          })            
       // }
      })
    },
    getOpenId: function(t) {
        return new Promise((rs, rj)=> {
            wx.login({success: (res)=> {
                if (res.code) {
                    //发起网络请求
                    //console.log(this.domain + "/ssm/appWebServ/getOpenId")
                    wx.request({
                      url: this.domain + "/ssm/appWebServ/getOpenId",
                      data: {
                        code: res.code
                      },
                      success: (d)=> {
                          //console.log(d)
                          //rs(d);
                          //console.log(d.data.openid)
                          this.openid=d.data.openid
                          //console.log(this.openid)
                          var that=this
                          wx.getWeRunData({
                            success (res) {
                              // 拿 encryptedData 到开发者后台解密开放数据
                              //console.log(res)
                              const encryptedData = res.encryptedData
                              //console.log(encryptedData)
                              // 或拿 cloudID 通过云调用直接获取开放数据
                              const cloudID = res.cloudID
                              
                              wx.request({
                                url: that.domain + "ssm/appWebServ/saveStepInfo",
                                data: {
                                  data: res,
                                  openid: that.openid
                                },
                                header: {
                                  'content-type': 'application/json' // 默认值
                                },
                                success (res) {
                                 // console.log(res)
                                },
                                complete (r) {
                      
                                }
                              })
                            },
                            fail(res) {
                              // wx.showModal({
                              //   title: '提示',
                              //   content: '请先开启微信运动'+ res.errMsg,
                              //   showCancel:false,
                              //   success (res) {
                              //     if (res.confirm) {
                              //       console.log('用户点击确定')
                              //     }
                              //   }
                              // })
                            }

                          })
                      },
                      fail() {
                          rj("error")
                      }
                    })
                  } else {
                    rj('登录失败！' + res.errMsg)
                    // wx.showModal({
                    //   title: '提示',
                    //   content: '微信登陆失败'+ res.errMsg,
                    //   showCancel:false,
                    //   success (res) {
                    //     if (res.confirm) {
                    //       console.log('用户点击确定')
                    //     }
                    //   }
                    // })
                  }
            },
            fail(res) {
              // wx.showModal({
              //   title: '提示',
              //   content: '微信登陆失败'+ res.errMsg,
              //   showCancel:false,
              //   success (res) {
              //     if (res.confirm) {
              //       console.log('用户点击确定')
              //     }
              //   }
              // })
            }
          });
        })


    },
    getUserInfo: function(t) {
      if(!this.userInfo) {
        let d = wx.getStorageSync('userInfo');
        if(d) {
            this.userInfo = d;
            return d;
        }
        return null;
      } 
      return this.userInfo; 
    },
    onPageNotFound: function(t) {
        console.log("页面不存在的res", t), wx.showModal({
            title: "页面不存在",
            content: t.path,
            showCancel: !1,
            confirmText: "到首页",
            success: function() {
                wx.redirectTo({
                    url: "pages/index/index"
                });
            }
        });
    }
});